home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Calc 1.24.7 / calc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-12  |  5.7 KB  |  276 lines  |  [TEXT/R*ch]

  1. /*
  2.  * Copyright (c) 1992 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Arbitrary precision calculator.
  7.  */
  8.  
  9. #include <signal.h>
  10.  
  11. #ifndef MACINTOSH
  12. #include <pwd.h>
  13. #include <sys/types.h>
  14. #else
  15. #include <stdio.h>
  16. #include <unix.h>
  17. #include <console.h>
  18. #endif
  19.  
  20. #include "calc.h"
  21. #include "func.h"
  22. #include "opcodes.h"
  23. #include "config.h"
  24. #include "token.h"
  25. #include "symbol.h"
  26.  
  27. /*
  28.  * Common definitions
  29.  */
  30. long maxprint;        /* number of elements to print */
  31. int abortlevel;        /* current level of aborts */
  32. BOOL inputwait;        /* TRUE if in a terminal input wait */
  33. jmp_buf jmpbuf;        /* for errors */
  34.  
  35. static int q_flag = FALSE;    /* TRUE => don't execute rc files */
  36.  
  37. char *calcpath;        /* $CALCPATH or default */
  38. char *calcrc;        /* $CALCRC or default */
  39. char *home;        /* $HOME or default */
  40. static char *pager;    /* $PAGER or default */
  41. char *shell;        /* $SHELL or default */
  42.  
  43. static void intint();    /* interrupt routine */
  44. void givehelp();
  45. static void initenv();    /* initialize/default special environment vars */
  46.  
  47.  
  48. extern char *getenv();
  49. #ifndef MACINTOSH
  50. extern struct passwd *getpwuid();
  51. extern uid_t geteuid();
  52. #endif
  53.  
  54. /*
  55.  * Top level calculator routine.
  56.  */
  57. main(argc, argv)
  58.     char **argv;
  59. {
  60.     char *str;        /* current option string or expression */
  61.     char cmdbuf[MAXCMD+1];    /* command line expression */
  62.  
  63. #ifdef MACINTOSH
  64.     console_options.pause_atexit = 0;
  65.     argc = ccommand(&argv);
  66. #endif /* MACINTOSH */
  67.  
  68.     initenv();
  69.     argc--;
  70.     argv++;
  71.     while ((argc > 0) && (**argv == '-')) {
  72.         for (str = &argv[0][1]; *str; str++) switch (*str) {
  73.             case 'h':
  74.                 givehelp(DEFAULTCALCHELP);
  75.                 exit(0);
  76.                 break;
  77.             case 'q':
  78.                 q_flag = TRUE;
  79.                 break;
  80.             default:
  81.                 printf("Unknown option\n");
  82.                 exit(1);
  83.         }
  84.         argc--;
  85.         argv++;
  86.     }
  87.     str = cmdbuf;
  88.     *str = '\0';
  89.     while (--argc >= 0) {
  90.         *str++ = ' ';
  91.         strcpy(str, *argv++);
  92.         str += strlen(str);
  93.         str[0] = '\n';
  94.         str[1] = '\0';
  95.     }
  96.     str = cmdbuf;
  97.     if (*str == '\0') {
  98.         str = NULL;
  99.         printf("C-style arbitrary precision calculator.\n");
  100.         version(stdout);
  101.         printf("[Type \"exit\" to exit, or \"help\" for help.]\n\n");
  102.     }
  103.     if (setjmp(jmpbuf) == 0) {
  104.         initmasks();
  105.         inittokens();
  106.         initglobals();
  107.         initfunctions();
  108.         initstack();
  109.         resetinput();
  110.         cleardiversions();
  111.         setfp(stdout);
  112.         setmode(MODE_INITIAL);
  113.         setdigits(DISPLAY_DEFAULT);
  114.         maxprint = MAXPRINT_DEFAULT;
  115.         _epsilon_ = atoq(EPSILON_DEFAULT);
  116.         _epsilonprec_ = qprecision(_epsilon_);
  117.         if (str) {
  118.             if (q_flag == FALSE) {
  119.                 runrcfiles();
  120.                 q_flag = TRUE;
  121.             }
  122.             (void) openstring(str);
  123.             getcommands();
  124.             exit(0);
  125.         }
  126.     }
  127.     if (str)
  128.         exit(1);
  129.     abortlevel = 0;
  130.     _math_abort_ = FALSE;
  131.     inputwait = FALSE;
  132.     (void) signal(SIGINT, intint);
  133.     cleardiversions();
  134.     setfp(stdout);
  135.     resetinput();
  136.     if (q_flag == FALSE) {
  137.         runrcfiles();
  138.         q_flag = TRUE;
  139.     }
  140.     (void) openterminal();
  141.     getcommands();
  142.     exit(0);
  143.     /*NOTREACHED*/
  144. }
  145.  
  146.  
  147. /*
  148.  * initenv - obtain $CALCPATH, $CALCRC, $HOME, $PAGER and $SHELL values
  149.  *
  150.  * If $CALCPATH, $CALCRC, $PAGER or $SHELL do not exist, use the default
  151.  * values.  If $PAGER or $SHELL is an empty string, also use a default value.
  152.  * If $HOME does not exist, or is empty, use the home directory
  153.  * information from the password file.
  154.  */
  155. static void
  156. initenv()
  157. {
  158.     struct passwd *ent;        /* our password entry */
  159.  
  160.     /* determine the $CALCPATH value */
  161.     calcpath = getenv(CALCPATH);
  162.     if (calcpath == NULL)
  163.         calcpath = DEFAULTCALCPATH;
  164.  
  165.     /* determine the $CALCRC value */
  166.     calcrc = getenv(CALCRC);
  167.     if (calcrc == NULL) {
  168.         calcrc = DEFAULTCALCRC;
  169.     }
  170.     
  171.     /* determine the $HOME value */
  172.     home = getenv(HOME);
  173.     if (home == NULL || home[0] == '\0') {
  174. #ifndef MACINTOSH    
  175.         ent = getpwuid(geteuid());
  176. #else
  177.         ent =NULL;
  178. #endif        
  179.         if (ent == NULL) {
  180.             /* just assume . is home if all else fails */
  181. #ifndef MACINTOSH        
  182.             home = ".";
  183. #else
  184.             home = ":";
  185. #endif    
  186.         }
  187. #ifndef MACINTOSH        
  188.         home = (char *)malloc(strlen(ent->pw_dir)+1);
  189.         strcpy(home, ent->pw_dir);            
  190. #else
  191.         home = ":";
  192. #endif            
  193.     }
  194.  
  195.     /* determine the $PAGER value */
  196.     pager = getenv(PAGER);
  197.     if (pager == NULL || *pager == '\0') {
  198.         pager = DEFAULTCALCPAGER;
  199.     }
  200.  
  201.     /* determine the $SHELL value */
  202.     shell = getenv(SHELL);
  203.     if (shell == NULL)
  204.         shell = DEFAULTSHELL;
  205. }
  206.  
  207. void
  208. givehelp(type)
  209.     char *type;        /* the type of help to give, NULL => index */
  210. {
  211.     char *helpcmd;        /* what to execute to print help */
  212. #ifdef MACINTOSH
  213.     FILE *f;
  214.     char fn[32], line[100];
  215.     int  c;
  216. #endif
  217.     
  218.     /* catch the case where we just print the index */
  219.     if (type == NULL) {
  220.         type = DEFAULTCALCHELP;        /* the help index file */
  221.     }
  222. #ifndef MACINTOSH
  223.     /* form the help command name */
  224.     helpcmd = (char *)malloc(
  225.         sizeof("if [ ! -d \"")+sizeof(HELPDIR)+1+strlen(type)+
  226.         sizeof("\" ];then ")+
  227.         strlen(pager)+1+1+sizeof(HELPDIR)+1+strlen(type)+1+1+
  228.         sizeof(";else echo no such help;fi"));
  229.     sprintf(helpcmd, 
  230.         "if [ -r \"%s/%s\" ];then %s \"%s/%s\";else echo no such help;fi", 
  231.         HELPDIR, type, pager, HELPDIR, type);
  232.  
  233.     /* execute the help command */
  234.     system(helpcmd);
  235.     free(helpcmd);
  236. #else
  237.     /* a simple pager here.   t.s.yang 10/12/93  UC Berkeley */
  238.     sprintf(fn,":help:%s",type);
  239.     f= fopen(fn,"r");
  240.     if (!f) {
  241.        printf("No such help.\n");
  242.        return;
  243.     }
  244.     c = 0;
  245.     while(!feof(f)) {
  246.         fgets(line,80,f);
  247.         printf("%s",line);
  248.         c++;
  249.         if (c % 24 == 0) {
  250.            printf("%s","Press <return>");
  251.            gets(line);
  252.         }
  253.     }
  254.     fclose(f);
  255. #endif    
  256. }
  257.  
  258.  
  259. /*
  260.  * Interrupt routine.
  261.  */
  262. /*ARGSUSED*/
  263. static void
  264. intint(arg)
  265.     int arg;    /* to keep ANSI C happy */
  266. {
  267.     (void) signal(SIGINT, intint);
  268.     if (inputwait || (++abortlevel >= ABORT_NOW))
  269.         error("\nABORT");
  270.     if (abortlevel >= ABORT_MATH)
  271.         _math_abort_ = TRUE;
  272.     printf("\n[Abort level %d]\n", abortlevel);
  273. }
  274.  
  275. /* END CODE */
  276.